#!/bin/bash
# Install additional files into the runtime Cygwin environment and/or update
# existing files where necessary.

ENV_RUNTIME_DIR="$1"
CYGWIN_EXTRAS="$2"
SAGE_VERSION="$3"

if [ -z "$ENV_RUNTIME_DIR" -o -z "$CYGWIN_EXTRAS" -o -z "$SAGE_VERSION" ]; then
    # Be extra safe here; e.g. if $CYGWIN_EXTRAS is missing then the next
    # line will try to copy all of /
    echo "sage-prep-runtime-extras missing arguments" >&2
    exit 1
fi

cp -r "$CYGWIN_EXTRAS"/* "$ENV_RUNTIME_DIR"
echo "SAGE_VERSION=$SAGE_VERSION" > "${ENV_RUNTIME_DIR}/etc/sage-version"

NSSWITCH="${ENV_RUNTIME_DIR}/etc/nsswitch.conf"
NSSWITCH_LINE='db_home: /home/sage'
if ! grep "$NSSWITCH_LINE" "$NSSWITCH" > /dev/null; then
	echo "$NSSWITCH_LINE" >> "$NSSWITCH"
fi

# Set the package download cache to something sensible; otherwise it
# will hard-code the package download cache used when building the
# environment in the installer, which is not what we want
# See https://github.com/sagemath/sage-windows/issues/24
sed -i '/^last-cache/{n;s|.*|\t/tmp|;}' "${ENV_RUNTIME_DIR}/etc/setup/setup.rc"
